package com.wuyi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuyi.mapper.CollectMapper;
import com.wuyi.mapper.FollowMapper;
import com.wuyi.mapper.ResourceMapper;
import com.wuyi.pojo.Collect;
import com.wuyi.pojo.Follow;
import com.wuyi.pojo.Resource;
import com.wuyi.pojo.bo.CollectBO;
import com.wuyi.pojo.vo.ResourceVO;
import com.wuyi.service.CollectService;
import com.zc.utils.JSONResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName CollectServiceImpl.java
 * @Description 收藏实现类
 * @createTime 2021年11月04日 11:05:00
 */
@Service
public class CollectServiceImpl implements CollectService {


    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private FollowMapper followMapper;

    @Transactional( propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo list(String userId, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(Collect.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("collectUid", userId);
        List<Collect> collects = collectMapper.selectByExample(example);

        //查出全部资源
        List<Resource> resourceList = resourceMapper.selectAll();

        List<ResourceVO> resourceVOList = new ArrayList<>();
        //循环遍历对比resourceId
        for (Collect collect : collects) {
            for (Resource resource : resourceList) {
                if (collect.getCollectRid().equals(resource.getResourceId())) {
                    //设置ResourceVO
                    ResourceVO resourceVO = new ResourceVO();
                    BeanUtils.copyProperties(resource, resourceVO);
                    resourceVO.setIsCollected(1);
                    resourceVOList.add(resourceVO);
                    break;
                }
            }
        }

        //查询全部关注
        List<Follow> followList = followMapper.selectAll();
        for (Follow follow : followList) {
            for (ResourceVO resourceVO : resourceVOList) {

            }
        }


        PageInfo pageInfo = new PageInfo<>(collects);
        pageInfo.setList(collects);

        return pageInfo;
    }

    /**
     * 收藏资源
     * @param collectBO
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED)
    @Override
    public JSONResult add(CollectBO collectBO) {
        //先查询是否被收藏，若被收藏则返回已收藏
        Collect collect = new Collect();
        collect.setCollectUid(collectBO.getUserName());
        collect.setCollectRid(collectBO.getResourceId());
        int i = collectMapper.selectCount(collect);
        if (i > 0) {
            return JSONResult.errorMsg("该资源已收藏");
        }
        //查询资源是否存在
        Resource resource = new Resource();
        resource.setResourceId(collectBO.getResourceId());
        int resCout = resourceMapper.selectCount(resource);
        if (resCout == 0) {
            return JSONResult.errorMsg("不存在该资源");
        }

        collect.setCreateTime(new Date());
        collect.setUpdateTime(new Date());
        int res = collectMapper.insert(collect);
        if (res != 1) {
            return JSONResult.errorMsg("收藏失败");
        }

        return JSONResult.ok("收藏成功");
    }

    /**
     * 取消收藏
     * @param collectBO
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED)
    @Override
    public JSONResult cancel(CollectBO collectBO) {

        Example example = new Example(Collect.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("collectUid", collectBO.getUserName());
        criteria.andEqualTo("collectRid", collectBO.getResourceId());
        Collect collect = collectMapper.selectOneByExample(example);
        int i = collectMapper.deleteByPrimaryKey(collect.getCollectId());
        if (i != 1) {
            return JSONResult.errorMsg("取消收藏失败");
        }
        return JSONResult.ok("取消收藏成功");
    }


}
